<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class facturas extends CI_Controller {

    public function main() {
        $this->load->helper('form');

        $this->load->library('AucarAssets');
        $assets = new AucarAssets;
        $data['assets'] = $assets->print_assets();
        $this->load->library('AucarMenu');
        $menu = new AucarMenu;
        $data['menu'] = $menu->print_menu();
        $this->load->view('facturas/home', $data);
    }

    public function generate($param) {
        $this->load->library('fpdf');
        $mes = $this->input->post('mes');
        $anyo = $this->input->post('anyo');
        $colaboradores_id = $this->input->post('colaboradores_id');
        $query = $this->db->get_where('colaboradores', array('id' => $colaboradores_id));
        $colaborador = $query->row();
        $emisor = array('nombre' => $this->config->item('nombre'), 'direccion' => $this->config->item('direccion'));

        $this->db->select('justificantes.comision as comision,
            justificantes.mes,
            justificantes.anyo,
            polizas.npoliza as numero_poliza,
            clientes.nombre as cliente_nombre,
              clientes.first_apellido as first_apellido,
                 clientes.last_apellido as last_apellido,
            colaboradores.nombre as colaborador,
            colaboradores.id as colaboradores_id');
        $this->db->from('colaboradores');
        $this->db->join('polizas', 'polizas.colaboradores_id = colaboradores.id');
        $this->db->join('clientes', 'clientes.id = polizas.clientes_id');
        $this->db->join('justificantes', 'justificantes.polizas_id = polizas.id');
        $this->db->where('anyo', $anyo);
        $this->db->where('mes', $mes);
        $this->db->where('colaboradores_id', $colaboradores_id);

        $query = $this->db->get();
        ob_end_clean();
        $this->fpdf->Open();
        $this->fpdf->AddPage();
        $this->fpdf->SetTextColor(50, 60, 100);
        $this->fpdf->SetXY(10, 50);
        $this->fpdf->SetFontSize(10);
        $this->fpdf->Image('img/logo/logo.jpg', 10, 0, 0);
        $this->fpdf->SetXY(10, 20);
        $this->fpdf->Write(5, $emisor['nombre']);
        $this->fpdf->SetXY(10, 25);
        $this->fpdf->Write(5, $emisor['direccion']);
        $this->fpdf->SetXY(150, 20);
        $this->fpdf->Write(5, $colaborador->nombre);
        $this->fpdf->SetXY(150, 25);
        $this->fpdf->Write(5, 'Factura: ' . $mes . '-' . $anyo);
        $y = 50;
		$total=0;
		
        foreach ($query->result() as $row) {
            $this->fpdf->SetXY(10, $y + 5);
            $this->fpdf->Write(5, utf8_decode('Cliente:' . $row->cliente_nombre . ' ' . $row->first_apellido . ' ' . $row->last_apellido));
            $this->fpdf->SetXY(10, $y + 10);
            $this->fpdf->Write(5, utf8_decode('Poliza:' . $row->numero_poliza));
            $this->fpdf->SetXY(10, $y + 15);
            $this->fpdf->Write(5, 'Comision:' . $row->comision/2);
            $y = $y + 15;
			$total=$total+$row->comision/2;
              
		}
        $this->fpdf->setXY(0,200);
		  $this->fpdf->Write(5, 'Comision total:' . $total);
		  $descuento=$total*25/100;
		  $neto=$total-$descuento;
		  $this->fpdf->setXY(0,205);
		  $this->fpdf->Write(5, 'Descuento:' . $descuento);
		  $this->fpdf->setXY(0,210);
		  $this->fpdf->Write(5, 'Neto:' . $neto);

        $this->fpdf->Output($mes.'-'.$anyo.'-'.$colaborador->nombre.'.pdf','D');

    
    }

}

/* End of file welcome.php */
/* Location: ./application/controllers/facturas.php */